/*
 *
 * I (David P Shenba) licenses this file to you under the Apache License, 
 * Version 2.0 (the "License"); you may not use this 
 * file except in compliance with the License.  You may 
 * obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.shenba.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import javax.servlet.jsp.PageContext;

import org.apache.commons.beanutils.PropertyUtils;

/**
 * This class is a singleton class for grabbing object from 
 * page context (in our case, the ArrayList handling)
 * @author David P Shenba
 * @version 1.0
 */
public class ObjectGrabber {
	
	/**
	 * The only constructor
	 */
	private ObjectGrabber() {
		//To avoid instance creation
	}
	
	/**
	 * Singleton instance
	 */
	private static ObjectGrabber instance = null;
	
	/**
	 * This method returns an instance of ObjectGrabber
	 * @return objectGrabber instance
	 */
	public static ObjectGrabber getInstance() {
		if(instance == null) {
			instance = new ObjectGrabber();
		}
		return instance;
	}
	
	/**
	 * This method can be improved!!!
	 * @param pageContext
	 * @param name
	 * @return the form object
	 * @throws NullPointerException if the name of the form
	 * 		                        is empty
	 */
	public Object getForm(PageContext pageContext, String name) 
		throws NullPointerException{
		//returns the form object in ANY scope
		if(name == null) {
			throw new NullPointerException("Tag attribute -name- is null");
		}
		return pageContext.findAttribute(name);
	}
	
	/**
	 * This method may be made common in future!!!
	 * dependent on common-beanutils by APACHE!!!
	 * to be written on our own?!
	 * @param form
	 * @param property
	 * @return
	 * @throws ClassCastException
	 * @throws NullPointerException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	public ArrayList getArrayFromForm(Object form, String property) 
		throws ClassCastException, IllegalAccessException, 
		InvocationTargetException, NoSuchMethodException{
		//returns the ArrayList object from form
		if(property == null) {
			throw new NoSuchMethodException("Tag attribute -property- is null");
		}
		return (ArrayList)PropertyUtils.getSimpleProperty(form, property);
	}
	
	/**
	 * Returns a string (tries even primitives)
	 * @param bean
	 * @param property
	 * @return string of the property
	 * @throws ClassCastException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	public String getStringFromBean(Object bean, String property) 
		throws ClassCastException, IllegalAccessException, 
		InvocationTargetException, NoSuchMethodException{
		
		String attribValue = null; 
		//returns string from bean
		if(property == null) {
			throw new NoSuchMethodException("Tag attribute -property- is null");
		}
		try {
		  attribValue = (String)PropertyUtils.getSimpleProperty(bean, property);
		} catch (Exception e){
			attribValue = ""+PropertyUtils.getSimpleProperty(bean, property);
		}
		return attribValue;
	}
}
